package com.lebang.common;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Util {

	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public static List<Map<String, Object>> readXlsx(InputStream inputStream) {
		try {
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			Workbook workbook = new XSSFWorkbook(inputStream);
			Sheet sheet = workbook.getSheetAt(0);
			// 获得行数
			int rows = sheet.getLastRowNum() + 1;
			Row tmp = sheet.getRow(0);
			if (tmp == null) {
				return null;
			}
			int cols = tmp.getPhysicalNumberOfCells();
			// 读取数据
			for (int row = 1; row < rows; row++) {
				try {
					Row r = sheet.getRow(row);
					Map<String, Object> rowMap = new LinkedHashMap<String, Object>();
					for (int col = 0; col < cols; col++) {
						if (null == tmp.getCell(col).getStringCellValue()) {
							continue;
						}
						if (null == r.getCell(col)) {
							rowMap.put(tmp.getCell(col).getStringCellValue(), "");
						} else if (r.getCell(col).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
							if (HSSFDateUtil.isCellDateFormatted(r.getCell(col))) {
								SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
								rowMap.put(tmp.getCell(col).getStringCellValue(),
										sdf.format(HSSFDateUtil.getJavaDate(r.getCell(col).getNumericCellValue()))
												.toString());
							} else {
								rowMap.put(tmp.getCell(col).getStringCellValue(), r.getCell(col).getNumericCellValue());
							}
						} else if (r.getCell(col).getCellType() == HSSFCell.CELL_TYPE_STRING) {
							rowMap.put(tmp.getCell(col).getStringCellValue(), r.getCell(col).getStringCellValue());
						} else if (r.getCell(col).getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
							rowMap.put(tmp.getCell(col).getStringCellValue(), r.getCell(col).getBooleanCellValue());
						} else if (r.getCell(col).getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
							rowMap.put(tmp.getCell(col).getStringCellValue(), r.getCell(col).getCellFormula());
						} else {
							rowMap.put(tmp.getCell(col).getStringCellValue(), r.getCell(col).getStringCellValue());
						}
					}
					boolean insert = false;
					for (String key : rowMap.keySet()) {
						if (null != rowMap.get(key) && !"".equals(rowMap.get(key))) {
							insert = true;
						}
					}
					if (insert) {
						data.add(rowMap);
					}
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}
			}
			return data;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
